/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.List;
import javax.ejb.Stateful;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import model.Client;

/**
 * This class is used to do operation on client, and to do high level process on client
 * @author Guillaume,yann
 */
@Stateful
@LocalBean
public class UserFacade {

    @PersistenceContext(unitName = "marketPU")
    private EntityManager em;
    
    /**
     * Create a new user that does not exist in the database
     * @param name name of the new client
     * @param password password of the new client
     * @return return the object client created for the new client
     * @throws Exception exception if the name already exist
     */
    public Client createUser(String name, String password) throws Exception{
        // Check if the client already exist
        Query queryFrom = em.createNamedQuery("findUser",Client.class);
        queryFrom.setParameter("name", name);
        List<Client> clientList = queryFrom.getResultList();
        if (clientList.size() > 0) {
            throw new Exception("Name already exist for: " + name);
        }
        
        Client client = new Client(name, password);
        em.persist(client);
        return client;
    
    }
    
    /**
     * Get the client object with the requested name
     * @param name name of the requested client
     * @return return object of the requested client
     * @throws Exception exception if the account does not exist
     */
    public Client getClient(String name) throws Exception{
        Query queryFrom = em.createNamedQuery("findUser",Client.class);

        queryFrom.setParameter("name", name);
        List<Client> clientList = queryFrom.getResultList();
        if (clientList.size() < 1) {
            throw new Exception("No account with name " + name);
        }
        Client client = clientList.get(0);
        
        return client;
    }
    /**
     * Try to log a client with his name/password, search for client with name in the BDD and check
     * the password.
     * @param name          clientName
     * @param password      clientPassword
     * @return              the client object corresponding to the user name
     */
    public Client logging( String name, String password) throws Exception{
        
        Client client = this.getClient(name);
        
        // check the password
        if (!client.getPassword().equals(password)){
            throw new Exception("Wrong password");
        }
        
        return client;
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    /**
     * Search a list of all client
     * @return a list with all client
     */
    public List<Client> getListOfAllClient() {
        Query query = em.createNamedQuery("findAllUser", Client.class);
        List<Client> list = query.getResultList();
        return list;
    }
    /**
     * Change the right of one user
     * @param name name of the user which right will be change
     * @param grade new grade of the user
     * @param ban true=> ban the client
     * @throws Exception 
     */
    public void changeRight(String name, int grade, boolean ban) throws Exception{
        Client client = this.getClient(name);
        client.setGrade(grade);
        client.setBan(ban);
    }
    
    
}
